package myboot.myblog.controller.common;

import myboot.myblog.constant.PageConstant;
import myboot.myblog.service.BlogService;
import myboot.myblog.service.CategoryService;
import myboot.myblog.service.CommentService;
import myboot.myblog.service.ConfigurationService;
import myboot.myblog.service.LinkService;
import myboot.myblog.service.TagService;
import myboot.myblog.utils.PageQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author hujh
 * @date 2022/1/28 下午2:15
 * 页面请求跳转
 */
@Controller
public class AdminPageController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private TagService tagService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private ConfigurationService configurationService;

    /**
     * 跳转首页，将前端需要的字段都查询出来并存储到session当中
     */
    @GetMapping({"/admin", "/admin/index.html","/admin/index"})
    public String toIndex(HttpServletRequest request) {
        //1.获取session，将前端需要字段存放在session
        HttpSession session = request.getSession();
        //2.index首页显示字段设置
        session.setAttribute("totalBlog", blogService.totalBlog());
        session.setAttribute("totalComment", commentService.totalComment());
        session.setAttribute("totalCategory", categoryService.totalCategory());
        session.setAttribute("totalTag", tagService.totalTags());
        session.setAttribute("totalLink", linkService.totalLink());
        //3.系统配置信息
        session.setAttribute(PageConstant.CONFIGURATIONS, configurationService.findAllConfigs());
        //4.侧边栏激活
        request.setAttribute(PageConstant.PATH, "index");
        PageQueryUtil queryUtil = new PageQueryUtil(1, 1000, "asc", 0, null, null);
        //5.所有分类信息
        request.setAttribute(PageConstant.CATEGORY, categoryService.findCategories(queryUtil).getList());
        //6.跳转首页
        return PageConstant.ADMIN_INDEX;
    }

    /**
     * 跳转注册页
     */
    @GetMapping("/admin/toRegister")
    public String toRegister() {
        return "blog/amaze/register";
    }

    /**
     * 跳转登陆页
     */
    @GetMapping("/admin/login")
    public String toLogin() {
        return PageConstant.ADMIN_LOGIN;
    }

    /**
     * 跳转修改密码页面
     */
    @GetMapping("/admin/profile.html")
    public String profile(HttpServletRequest request) {
        request.setAttribute(PageConstant.PATH, PageConstant.PROFILE);
        return PageConstant.ADMIN_PROFILE;
    }

    /**
     * 跳转系统配置页面
     */
    @GetMapping("/admin/configuration.html")
    public String configuration(HttpServletRequest request) {
        request.setAttribute(PageConstant.PATH, PageConstant.CONFIGURATIONS);
        return PageConstant.ADMIN_CONFIGURATION;
    }

    /**
     * 跳转博客列表界面
     */
    @GetMapping("/admin/blog.html")
    public String showBlogs(HttpServletRequest request) {
        request.setAttribute(PageConstant.PATH, PageConstant.BLOG);
        return PageConstant.ADMIN_BLOG;
    }

    /**
     * 跳转分类首页
     */
    @GetMapping("/admin/category.html")
    public String toCategory(HttpServletRequest request) {
        request.setAttribute(PageConstant.PATH, PageConstant.CATEGORY);
        return PageConstant.ADMIN_CATEGORY;
    }

    /**
     * 跳转博客编辑页面
     */
    @GetMapping("/admin/blogs/edit.html")
    public String saveBlog(HttpServletRequest request) {
        request.setAttribute(PageConstant.PATH, PageConstant.EDIT);
        return "admin/edit";
    }

    /**
     * 跳转评论管理页面
     */
    @GetMapping("/admin/comment.html")
    public String toComments(HttpServletRequest request) {
        request.setAttribute(PageConstant.PATH, PageConstant.COMMENT);
        return PageConstant.ADMIN_COMMENT;
    }

    /**
     * 跳转链接页面
     */
    @GetMapping("/admin/link.html")
    public String toLink(HttpServletRequest request){
        request.setAttribute(PageConstant.PATH,PageConstant.LINK);
        return PageConstant.ADMIN_LINK;
    }

    /**
     * 跳转标签管理页面
     */
    @GetMapping("/admin/tag.html")
    public String toTags(HttpServletRequest request){
        request.setAttribute(PageConstant.PATH,PageConstant.TAG);
        return PageConstant.ADMIN_TAG;
    }

}
